package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.PowerBean;
import com.project.bean.UserBean;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class UsersService implements IUserService {
    @Autowired
    private IUserMapper userMapper;

    @Override
    public UserBean login(String name, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_name",name);
        qw.eq("u_pwd",pwd);
        System.out.println(userMapper.selectOne(qw)+"````````````````````");
        return userMapper.selectOne(qw);
    }

    /**
     * 添加用户
     * 添加用户，同时添加该用户拥有的角色
     * @param userName 用户名
     * @param realName 真实姓名
     * @param roleIdList 角色id集合
     */
    @Override
    public void addUser(String userName, String realName, List<Integer> roleIdList) {
        UserBean userBean = new UserBean(userName,realName);
        userMapper.insert(userBean);
        Integer id = userBean.getId();

        userMapper.addUserRole(id,roleIdList);
    }

    @Override
    public List<PowerBean> findByRoleIdList(List<Integer> roleIdList) {
        return userMapper.findByRoleIdList(roleIdList);
    }

    @Override
    public void delUser(Integer id) {
        userMapper.delUser(id);
    }

    @Override
    public IPage<UserBean> cutByUser(Integer pageNO, String userName) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (userName != null && !userName.equals("")) {
            qw.like("u_name",userName);
        }
        return userMapper.selectPage(new Page<>(pageNO, 2), qw);
    }

    @Override
    public UserBean findById(Integer userId) {
        return userMapper.findById(userId);
    }



    @Override
    public void update(Integer userId, List<Integer> roleIdList) {
        UserBean userBean = userMapper.selectById(userId);
        userMapper.del(userId);
        userMapper.addUserRole(userBean.getId(),roleIdList);
    }

    /**
     * 修改密码
     * 根据用户id修改密码
     * @param userId 用户id
     * @param newPwd 新密码
     */
    @Override
    public void updatePwd(Integer userId, String newPwd) {
        UserBean userBean = new UserBean();
        userBean.setPwd(newPwd);
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("pk_userId",userId);
        userMapper.update(userBean,qw);
    }

    @Override
    public UserBean findByPower(Integer userId) {
        UserBean userBean = userMapper.selectById(userId);
       List<PowerBean> list = userMapper.findByPower(userId);
       userBean.setPowerList(list);
        return userBean;
    }
}
